package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsDaoService;
import com.cy.pj.goods.service.GoodsDaoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/goods/")
@Controller
public class GoodsController {

    @Autowired
    GoodsDaoService goodsDaoService;

    @RequestMapping("findGoodsById/{id}")//从页面访问增加按钮时候首先使用id查询，将对应的商品信息查询出来，并在页面上显示
    public String findGoodsById(@PathVariable Integer id, Model model) {
        List<Goods> goodsList = goodsDaoService.findGoodsById(id);
        model.addAttribute("goodsList", goodsList);
        return "goods-update";
    }

    @RequestMapping("updateGoodsById")//这个功能用来实现商品信息的更新
    public String updateGoodsById(Goods goods) {
        goodsDaoService.updateGoods(goods);
        return "redirect:/goods/findGoods";
    }

    @RequestMapping("findGoods")//这个功能，查询所有商品信息，并显示到页面上
    public String showGoods(Model model) {
        List<Goods> goodsList = goodsDaoService.findGoods();
        model.addAttribute("goodList", goodsList);
        return "goods";
    }

    @RequestMapping("deleteById/{id}")//这个功能，根据id来进行商品的删除
    public String deleteById(@PathVariable Integer id) {
        goodsDaoService.deleteGoodsById(id);
        return "redirect:/goods/findGoods";//重定向
    }

    @RequestMapping("addGoods")//增加功能只是重定向页面到网页，不用进行数据库的增加，数据库的增加，通过网页submit提交进行
    public String addGoods() {

        System.out.println("asdas dsad ");
        return "goods-add";

    }

    @RequestMapping("saveGoods")//这个功能用来保存数据到数据库
    public String saveGoods(Goods goods) {
        goodsDaoService.insertGoods(goods);
        return "redirect:/goods/findGoods";
    }

}
